package com.huluspace.learnhub.service;

import com.huluspace.learnhub.dto.RoleDTO;
import com.huluspace.learnhub.model.Role;
import com.huluspace.learnhub.repository.RoleRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleService {

  private final RoleRepository roleRepository;

  public RoleService(RoleRepository roleRepository) {
    this.roleRepository = roleRepository;
  }

  public List<RoleDTO> getAvailableRoles() {
    return roleRepository.findByEnabledTrue()
        .stream()
        .map(role -> new RoleDTO(role.getName(), role.getDescription()))
        .collect(Collectors.toList());
  }

  public Role findByName(String name) {
    return roleRepository.findByName(name)
        .orElseThrow(() -> new IllegalArgumentException("角色不存在: " + name));
  }

  public List<Role> getAllRoles() {
    return roleRepository.findAll();
  }
}

